package com.graduation.sports.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by apc_allen
 * date : 1/20/21
 * description :
 */
public abstract class BaseActivity extends SupportActivity {
    protected LoadingDialog mLoadingDialog;
    protected Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        context = this;
        ButterKnife.bind(this);
        initDataAndEvent(savedInstanceState);
    }

    protected void showToast(String content){
        Toast.makeText(this,content, Toast.LENGTH_SHORT).show();
    }

    protected abstract int getLayout();

    protected abstract void initDataAndEvent(Bundle savedInstanceState);

    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    public void showLoadingDialog(final String str) {
        runOnUiThread(() -> {
            if (mLoadingDialog != null) {
                mLoadingDialog.close();
            }
            mLoadingDialog = null;
            if (isDestroyed() || isFinishing()) {
                return;
            }
            mLoadingDialog = new LoadingDialog(BaseActivity.this);
            if (!TextUtils.isEmpty(str)) {
                mLoadingDialog.setLoadingText(str);
            }
            mLoadingDialog.show();
        });
    }

    public void closeLoadingDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog != null) {
                    mLoadingDialog.close();
                }
            }
        });
    }

    protected void openActivity(Class<?> paramClass) {
        Intent localIntent = new Intent(this, paramClass);
        startActivity(localIntent);
    }


}
